# -*-encoding: Utf-8 -*-
# @File : assert_engine.py
# author: HJT
# Time：2024/3/30
from utils.log.logger import logger
from codes.hjt_assertion.basic_assert import BasicAssert


def validate_check(validate_list, __suffix_response, __suffix_request_params):
    """
    断言yaml用例中的一整个validate
    :param validate_list: validate断言表达式列表
    :param __suffix_response: 响应数据
    :param __suffix_request_params: 请求数据
    :return: None
    """
    fail_flags = 0
    if not validate_list:
        fail_flags = -1
    else:
        for ass_expr in validate_list:
            try:
                fail_flag = AssertTools(ass_expr, __suffix_response, __suffix_request_params).expr_check()
                fail_flags += fail_flag
            except:
                fail_flags += 1
    if fail_flags == -1:
        logger.info("没有任何断言")
    elif fail_flags == 0:
        logger.info("---------断言成功!!!---------")
    else:
        logger.info("---------断言失败!!!---------")
        raise AssertionError('断言失败，请检查日志！')


class AssertTools(BasicAssert):

    def __init__(self, ass_expr: dict, suffix_response, suffix_request_args):
        super().__init__(ass_expr, suffix_response, suffix_request_args)
        """
        self.ass_expr ——> e.g. {'equals': {'code': 200}}
        self.operator  ——> e.g. 'equals'
        self.expr ——> e.g. {'code': 200}
        self.suffix_response ——> 响应数据
        self.suffix_request_args ——> 请求数据
        self.expect_val ——> e.g. 'code'的jsonpath提取
        self.actual_val ——> e.g. 200
        self.ERROR_MSG ——> e.g. 框架不支持此断言！
        """

    # 状态码断言
    def codes_assert(self):
        if self.expect_val != self.actual_val:
            self.fail_flags += 1
        return self.fail_flags

    # 相等断言
    def equals_assert(self):
        if self.expect_val != self.actual_val:
            self.fail_flags += 1
        return self.fail_flags

    # 包含断言
    def contains_assert(self):
        if self.expect_val not in self.actual_val:
            self.fail_flags += 1
        return self.fail_flags

    # # 状态码断言
    # def codes_assert(self):
    #     key_, value_, fail_flag = self._hot_expr()
    #     if value_ != key_:
    #         fail_flag += 1
    #     return fail_flag
    #
    # # 相等断言
    # def equals_assert(self):
    #     key_, value_, fail_flag = self._hot_expr()
    #     if value_ != key_:
    #         fail_flag += 1
    #     return fail_flag
    #
    # # 包含断言
    # def contains_assert(self):
    #     key_, value_, fail_flag = self._hot_expr()
    #     if value_ not in key_:
    #         fail_flag += 1
    #     return fail_flag

    # 数据库断言
    def database_assert(self):
        pass
